<template>
  <div class="user-avatar">
    <img :src="images"
         class="img"
         alt=""
         ref="image">
    <div class="toolbar">
      <div class="cancel">取消</div>
      <div class="confirm"
           @click="onConfirm">完成</div>
    </div>
  </div>
</template>

<script>
import { updateUserInFormation } from '@/api/UserInformation.js'
export default {
  name: 'UserAvatar',
  props: {
    img: {
      type: [String, Object],
      required: true
    }
  },
  data () {
    return {
      images: this.img,
      cropper: null
    }
  },
  created () {

  },
  mounted () {
    const image = this.$refs.image
    var reader = new FileReader()
    // var blob = window.URL.createObjectURL(this.img)
    // reader.readAsArrayBuffer(blob)
    // reader.onload = function (e) {
    //   this.cropper = new Cropper(e.result, {
    //     viewMode: 1,
    //     dragMode: 'move',
    //     aspectRatio: 1,
    //     // autoCropArea: 1,
    //     cropBoxMovable: false,
    //     cropBoxResizable: false,
    //     background: false,
    //     movable: true
    //   })
    //   console.log(this.cropper)
    // }
  },
  methods: {
    onConfirm () {
      // console.log(this.cropper.getCroppedCanvas())
      // 纯客户端的裁切使用 getCroppedCanvas 获取裁切的文件对象
      // this.cropper.getCroppedCanvas().toBlob((blob) => {
      //   console.log(blob)
      //   this.updateUserAvatar(blob)
      // })
      this.$emit('update-photo', this.img)
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang="less">
.user-avatar {
  background-color: #000;
  height: 100%;
  .toolbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    .cancel,
    .confirm {
      width: 90px;
      height: 90px;
      font-size: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
    }
  }
}
.img {
  display: block;
  max-width: 100%;
}
</style>
